package com.wing.product.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.WarehouseProcessingDescription;
import com.wing.product.model.entity.WarehouseProcessingDescriptionPrice;
import com.wing.product.model.entity.WarehouseProductMark;
import com.wing.product.model.response.WarehouseProcessingDescriptionVO;
import com.wing.product.service.WarehouseProcessingDescriptionPriceService;
import com.wing.product.service.WarehouseProcessingDescriptionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(value = "云仓", tags = "#/warehouse/product/warehouseProcessingDescription")
@RestController("WarehouseProcessingDescriptionController")
@RequestMapping("/warehouse/warehouseProcessingDescription")
@RequiredArgsConstructor
public class WarehouseProcessingDescriptionController {

    private final WarehouseProcessingDescriptionService warehouseProcessingDescriptionService;

    @Autowired
    private WarehouseProcessingDescriptionPriceService warehouseProcessingDescriptionPriceService;
    @ApiOperation(value = "查询所有", notes = "搜索")
    @GetMapping("/list")
    public PageResult<WarehouseProcessingDescriptionVO> list(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(value = "sort", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(value = "order", defaultValue = "asc", required = false) String order
            ,String markName
            ,String varietyName
            ,String customerName
            ,String isDelete
            ,String processingName
    ){

        Page<WarehouseProcessingDescriptionVO> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        paramMap.put("sort", StrUtil.toUnderlineCase(sort));
        paramMap.put("order", order);
        paramMap.put("markName", markName);//钢种
        paramMap.put("varietyName", varietyName);//品种
        paramMap.put("customerName", customerName);//客户
        paramMap.put("isDelete", isDelete);//状态 0正常 1禁用
        paramMap.put("processingName", processingName);//工艺

        //搜索
        IPage<WarehouseProcessingDescriptionVO> list = warehouseProcessingDescriptionService.pageList4app(pg,paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "添加", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@RequestBody WarehouseProcessingDescription warehouseProcessingDescription)  {
        try{
            return warehouseProcessingDescriptionService.saveWarehouseProcessingDescription(warehouseProcessingDescription);
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "删除", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Id", value = "加工描述id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long id) {
        return warehouseProcessingDescriptionService.deleteWarehouseProcessingDescription(id);
    }

    @ApiOperation(value = "修改", notes = "修改")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PutMapping("/update")
    public JsonResult update(@RequestBody WarehouseProcessingDescription warehouseProcessingDescription)  {
        try{
            return warehouseProcessingDescriptionService.updateWarehouseProcessingDescription(warehouseProcessingDescription);
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "添加", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/addProcessingPrice")
    public JsonResult addProcessingPrice(@RequestBody WarehouseProcessingDescriptionPrice warehouseProcessingDescriptionPrice)  {
        try{
            return warehouseProcessingDescriptionPriceService.saveWarehouseProcessingDescriptionPrice(warehouseProcessingDescriptionPrice);
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }


    @GetMapping("/priceList")
    public PageResult<Map> priceList(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(value = "sort", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(value = "order", defaultValue = "asc", required = false) String order
    ){

        Page<Map> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        paramMap.put("sort", StrUtil.toUnderlineCase(sort));
        paramMap.put("order", order);

        IPage<Map> list = warehouseProcessingDescriptionService.pageList4app(pg,paramMap);
        return new PageResult<Map>(list.getRecords(), list.getTotal());
    }

}
